package cn.tedu.sp06.controller;

import cn.tedu.item.sp01.pojo.Item;
import cn.tedu.item.sp01.pojo.Order;
import cn.tedu.item.sp01.pojo.User;
import cn.tedu.item.sp01.util.JsonResult;
import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.ws.rs.Path;
import java.sql.ResultSet;
import java.util.List;

@RestController
@Slf4j
public class RibbonController {
    @Autowired
    private RestTemplate rt;

    /**
     * 调用远程的商品服务，获得订单
     *
     * @param orderId
     * @return
     */



    public JsonResult<List<Item>> getItemFB( String orderId){
        return JsonResult.err().msg("获取订单失败");
    }

    public JsonResult<?> decreaseNumberFB( List<Item> items){
        return JsonResult.err().msg("减少库存失败");
    }


    public JsonResult<User> getUserFB( Integer userId){
        return JsonResult.err().msg("获取订单失败");
    }

    public  JsonResult<?> addScoreFB( Integer userId
            ,Integer score){
        return JsonResult.err().msg("获取订单失败");
    }


    public JsonResult<Order> getOrderFB( String orderId){
        return JsonResult.err().msg("获取订单失败");
    }


    public JsonResult addOrderFB(){
        return JsonResult.err().msg("获取订单失败");
    }




    @GetMapping("/item-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getItemFB")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId){
        return rt.getForObject("http://item-service/{1}",JsonResult.class,
                orderId );
    }
    @PostMapping("/item-service/decreaseNumber")
    @HystrixCommand(fallbackMethod = "decreaseNumberFB")
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> items){
        return rt.postForObject("http://item-service/decreaseNumber",
                items,JsonResult.class);
    }

    @GetMapping("/user-service/{userId}")
    @HystrixCommand(fallbackMethod = "getUserFB")
    public JsonResult<User> getUser(@PathVariable Integer userId){
        return rt.getForObject("http://user-service/{1}",
                JsonResult.class,userId);
    }
    @PostMapping("/user-service/{userId}/score")
    @HystrixCommand(fallbackMethod = "addScoreFB")
    public  JsonResult<?> addScore(@PathVariable Integer userId
    ,Integer score){
        return rt.getForObject("http://user-service/{1}/score?score={2}",
                JsonResult.class,userId,score);
    }

    @GetMapping("/order-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getOrderFB")
    public JsonResult<Order> getOrder(@PathVariable String orderId){
        return rt.getForObject("http://order-service/{1}",
                JsonResult.class,orderId);
    }

    @PostMapping("/order-service")
    @HystrixCommand(fallbackMethod = "addOrderFB")
    public JsonResult addOrder(){
        return rt.getForObject("http://order-service/",JsonResult.class);
    }

}
